package com.mxchip.livestar.av.camera;

import android.graphics.SurfaceTexture;
import android.media.MediaCodec;
import android.media.MediaFormat;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Surface;

import com.boegam.wirelessdisplay.ScreenPushNative;
import com.mxchip.livestar.av.player.PreviewOnSurfaceCreatedHandler;
import com.mxchip.livestar.utils.MyLog;
import com.mxchip.livestar.utils.Utils;
import com.mxchip.livestar.base.SystemData;
import com.mxchip.livestar.ui.call.AVDevice;
import com.videohigh.boegam.screenpush.ScreenPushManager;
import com.videohigh.boegam.screenpush.VideoEncoderCallback;

import java.io.IOException;
import java.nio.ByteBuffer;

import androidx.annotation.NonNull;

public class WifiCamera implements PreviewOnSurfaceCreatedHandler {

    public static final String DEVICE_NAME = AVDevice.CAMERA_WIFI;

    private static final String TAG = "WifiCamera";
    private EncoderCallback mEncoderCallback;

    public void setEncoderCallback(EncoderCallback mEncoderCallback) {
        this.mEncoderCallback = mEncoderCallback;
    }

    public void setDecodeTexture(SurfaceTexture surfaceTexture) {
        ScreenPushManager.getInstance().setVideoDecodeTexture(surfaceTexture);
    }

    public void setEncodeBitRate(int wifiBitRate) {
        ScreenPushManager.getInstance().setVideoEncoderBitRate(wifiBitRate);
    }

    private static WifiCamera mInstance = new WifiCamera();

    public static synchronized WifiCamera getInstance() {
        return mInstance;
    }

    private WifiCamera() {
    }

    public void start() {
        ScreenPushManager.getInstance().setVideoEncoderCallback(videoEncoderCallback);
        // ScreenPushManager.getInstance().startVideoDecode();
    }

    public void stop() {
        // ScreenPushManager.getInstance().stopVideoDecode();
        ScreenPushManager.getInstance().stopVideoDecode();
    }

    private VideoEncoderCallback videoEncoderCallback = new VideoEncoderCallback() {
        @Override
        public void onGetH264Frame(@NonNull byte[] data, int length, int width, int height) {
            if (SystemData.getInstance().getWifiResPub()) {
                if(mEncoderCallback != null) {
                    mEncoderCallback.onH264FramePacket(data, DEVICE_NAME, width, height, 0, System.currentTimeMillis());
                }
            }
        }
    };

    public void release() {
         ScreenPushManager.getInstance().releaseVideo();
        mEncoderCallback = null;
    }
}
